package test

import scala.annotation.tailrec

object Hof {

  def factorial(n: Int): Int = {
    @tailrec
    def go(n: Int, acc: Int): Int = {
      if (n <= 0) acc
      else go(n - 1, n * acc)
    }
    go(n, 1)
  }

  def formatFactorial(n: Int): Unit ={
    val msg = "The factorial of %d is %d."
    println(msg.format(n, factorial(n)))
  }

  def formatResult(name: String, n: Int, f: Int => Int) ={
    val msg = "The %s of %d is %d."
    println(msg.format(name, n, f(n)))
  }

  def main(args: Array[String]): Unit = {
    formatFactorial(7)
    formatResult("factorial", 7, factorial)
  }
}
